#!/usr/bin/env python
# -*- coding: utf-8 -*-


import pysvn

import ConfigModule as c_m
import SvnUtillity as s_u


class ProjectConfig:
    def __init__(self, project):
        self.project = project
        self.client = pysvn.Client()

    def is_svn_project(self):
        return s_u.is_svn_path(self.project.path)

    # check out 项目
    def check_out(self):
        client = self.client
        prj = self.project
        path = prj.path
        try:
            client.checkout(prj.url, path)
            client.callback_get_login = lambda (realm, username, may_save): True, prj.usr, prj.pwd, False
        except Exception, e:
            print e
            return False
        return True


class ProjectContext:
    def __init__(self):
        self.config_parser = parser = c_m.ConfigParser()
        self.prj_dic = prj_dic = {}
        prj_lst = parser.get_project_list()
        for prj in prj_lst:
            prj_config = parser.get_prj_config(prj)
            prj_dic[prj] = ProjectConfig(prj_config)

    # 判断指定的项目是否为svn项目
    def is_svn_project(self, project_name):
        prj_dic = self.prj_dic
        return prj_dic.get(project_name).is_svn_project() if prj_dic.get(project_name) is not None else False

